package com.vsu.bookstore.mvc.ui.widgets;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import com.jensjansson.pagedtable.PagedTable;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.ui.VerticalLayout;
import com.vsu.bookstore.mvc.model.bean.BookStoreItem;
import com.vsu.bookstore.mvc.ui.AbstractView;
import com.vsu.bookstore.util.DatabaseHelper;

@SuppressWarnings("serial")
public class TableContainer extends AbstractView
{
  private final VerticalLayout rootLayout = new VerticalLayout();

  private final Map<Object, TablePanel> panelMap = new HashMap<Object, TablePanel>();

  private boolean singleVisible = false;

  public static final String MULTI_TABLE_UNIT_HEIGHT = "900px";

  public static final String SINGLE_TABLE_UNIT_HEIGHT = "2000px";

  public TableContainer()
  {
    super();
    setSizeUndefined();
    setWidth("100%");
    rootLayout.addStyleName("light");
    rootLayout.setWidth("100%");
    rootLayout.setSpacing(true);
    final Container container = DatabaseHelper.get().getCategoryContainer();
    final Collection<?> categories = container.getItemIds();
    final Iterator<?> it = categories.iterator();
    while (it.hasNext())
    {
      final Item item = container.getItem(it.next());
      final String title = (String)item.getItemProperty("Title").getValue();
      final Object id = item.getItemProperty("ID").getValue();
      final TablePanel panel = new TablePanel(id, title);
      rootLayout.addComponent(panel);
      panelMap.put(id, panel);
    }
    setCompositionRoot(rootLayout);
  }

  public void changeVisibilyState(final Object id)
  {
    if (singleVisible)
      showAll();
    else
      showSingleCategory(id);
  }

  public void onCategoryUpdated(final BookStoreItem bean)
  {
    final Integer categoryId = bean.getCategoryId();
    final PagedTable table = panelMap.get(categoryId).getTable();
    final int page = table.getCurrentPage();
    if (page == table.getTotalAmountOfPages())
      table.nextPage();
    else
      table.setCurrentPage(table.getTotalAmountOfPages());
  }

  @Override
  protected void onView()
  {
  }

  private void showAll()
  {
    rootLayout.removeAllComponents();
    final Set<Entry<Object, TablePanel>> entries = panelMap.entrySet();
    final Iterator<Entry<Object, TablePanel>> it = entries.iterator();
    while (it.hasNext())
    {
      final Entry<Object, TablePanel> entry = it.next();
      entry.getValue().getContent().setHeight(MULTI_TABLE_UNIT_HEIGHT);
      entry.getValue().getTable().setTablePageLength(BookStoreTable.DEFAULT_PAGE_LENGTH);
      rootLayout.addComponent(entry.getValue());
    }
    singleVisible = false;
  }

  private void showSingleCategory(final Object id)
  {
    singleVisible = true;
    final TablePanel panel = panelMap.get(id);
    rootLayout.removeAllComponents();
    rootLayout.addComponent(panel);
    panel.getTable().setTablePageLength(BookStoreTable.SINGLE_MODE_PAGE_LENGTH);
    panel.getContent().setHeight(SINGLE_TABLE_UNIT_HEIGHT);
  }
}
